﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace FileParser
{
    /// <summary>
    /// This is a single - method class for interfacing with the
    /// system's command prompot for running external applications
    /// from within the program. The class's single function, 
    /// CMDCommand, holds all the functionality.
    /// </summary>
    class C_CMDUtil
    {
        /// <summary>
        /// This is the main function of CMDUtil, which is a static function
        /// for running command line style programs. This is used within
        /// the project for accessing SVN commands (preceded by 'svn')
        /// And the doxygen program ('doxygen'). It runs the window in
        /// the background, and can sometimes become dissociated with the
        /// application if it closes incorrectly.
        /// </summary>
        /// <param name="CMDArguments">
        /// This is the literal text of
        /// the command to be run. Example: "svn info http:/example.com/svn/" </param>
        /// <param name="waitmilli">
        /// This is an optional argument for specifying
        /// an amount of time in millseconds to wait before forcibly closing the window.
        /// If the value is less than or equal to zero, it does not use this feature.
        /// </param>
        /// <returns> 
        /// Returns a boolean representing the result of the program run
        /// true = program sucess, false = program failure or bad command
        /// </returns>
        public static bool CMDCommand(string CMDArguments, int waitmilli)
        {
            Process CMDProcess = new Process();
            ProcessStartInfo CMDStartInfo = new ProcessStartInfo();

            CMDStartInfo.FileName = "CMD.exe ";

            CMDStartInfo.RedirectStandardError = false;
            CMDStartInfo.RedirectStandardOutput = false;
            CMDStartInfo.RedirectStandardInput = false;

            CMDStartInfo.UseShellExecute = false;
            //Dont show a command window
            CMDStartInfo.CreateNoWindow = true;

            CMDStartInfo.Arguments = "/D /c " + CMDArguments;

            CMDProcess.EnableRaisingEvents = true;
            CMDProcess.StartInfo = CMDStartInfo;

            //start cmd.exe and the required infos process
            CMDProcess.Start();

            //set the wait period for exiting the process
            if (waitmilli > 0)
                CMDProcess.WaitForExit(waitmilli); //or the wait time you want
            else
                CMDProcess.WaitForExit();

            bool CMDSuccessful = true;

            int ExitCode = 0;

            //If the program fails to execute correctly,
            //this will throw a system invalid operation
            //exception, after which we try to recover
            // by killing the window and returning false.
            try
            {
                ExitCode = CMDProcess.ExitCode;
            }
            catch
            {
                CMDProcess.Kill();
                CMDSuccessful = false;
            }

            if (ExitCode != 0)
                CMDSuccessful = false;

            //now clean up after ourselves
            CMDProcess.Dispose();
            CMDStartInfo = null;
            return CMDSuccessful;
        }
    }
}
